package cn.com.landsky.camera.controller;

import cn.com.landsky.camera.common.hikvision.HikvisionEnums;
import cn.com.landsky.camera.common.utils.HikvisionUtil;
import cn.com.landsky.camera.entity.Camera;
import cn.com.landsky.camera.service.CameraService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/***
 ** @category 视频控制器...
 ** @author qing.yunhui
 ** @email: qingyunhui@fentuan360.com
 ** @createTime: 2018/12/25-11:54
 **/
@Controller
@RequestMapping(value="/camera")
@Slf4j
@Api(value = "CameraController", description = "摄像头控制器")
public class CameraController {

    @Value("${hikivision.ISC.host}")
    private String host;

    @Value("${hikivision.ISC.appKey}")
    private String appKey;

    @Value("${hikivision.ISC.appSecret}")
    private String appSecret;

    @Autowired
    Environment environment;

    @Autowired
    CameraService cameraService;

    private String cameraIndexCode="521959bb4c21409a828bc1f3d1063c05";

    //==============================================================> 视频应用服务demo begin ==============================================================>

    @RequestMapping(value = "/previewURL.htm",method ={RequestMethod.GET})
    @ApiOperation(value = "视频预览")
    public String previewURL(){
        return "preview";
    }

    @RequestMapping(value = "/playback.htm",method ={RequestMethod.GET})
    @ApiOperation(value = "视频回放")
    public String playback(){
        return "playback";
    }

    //==============================================================> begin ==============================================================>

    @RequestMapping(value = "/callRegions.htm",method ={RequestMethod.GET},produces = { "application/json;charset=UTF-8" })
    @ApiOperation(value = "分页获取区域列表为例")
    @ResponseBody
    public String callRegions(){
        String regions=HikvisionUtil.callRegions();
        log.info("regions:{}",new Object[]{regions});
        String result=jsonFormat(regions);
        return result;
    }

    @RequestMapping(value = "/callPreviewURLs.htm",method ={RequestMethod.GET},produces = { "application/json;charset=UTF-8" })
    @ApiOperation(value = "获取监控点预览取流URL")
    @ResponseBody
    public String callPreviewURLs(){
        String rtmpUrl=HikvisionUtil.callPreviewURLs(cameraIndexCode,HikvisionEnums.PROTOCOL.RTMP);
        String hlsUrl=HikvisionUtil.callPreviewURLs(cameraIndexCode,HikvisionEnums.PROTOCOL.HLS);
        String rtspUrl=HikvisionUtil.callPreviewURLs(cameraIndexCode);
        log.info("rtmpUrl:{},rtspUrl:{},hlsUrl:{}",new Object[]{rtmpUrl,rtspUrl,hlsUrl});
        String rtmpResult=jsonFormat(rtmpUrl);
        String rtspResult=jsonFormat(rtspUrl);
        String hlsResult=jsonFormat(hlsUrl);
        return rtmpResult+"\n"+rtspResult+"\n"+hlsResult;
    }

    @RequestMapping(value = "/controlling.htm",method ={RequestMethod.GET},produces = { "application/json;charset=UTF-8" })
    @ApiOperation(value = "根据监控点编号进行云台操作")
    @ResponseBody
    public String controlling(){
        String controlling=HikvisionUtil.callControlling(cameraIndexCode, HikvisionEnums.ACTION.START,HikvisionEnums.COMMAND.DOWN);
        log.info("controlling:{}",new Object[]{controlling});
        String result=jsonFormat(controlling);
        return result;
    }
    @RequestMapping(value = "/callPlaybackURL.htm",method ={RequestMethod.GET},produces = { "application/json;charset=UTF-8" })
    @ApiOperation(value = "获取监控点回放取流URL")
    @ResponseBody
    public String callPlaybackURL(){
        String beginTime="2019-01-10T15:10:00.000+08:00";
        String endTime="2019-01-10T15:13:00.000+09:00";
        String rtspPlaybackUrl=HikvisionUtil.callPlaybackURL(cameraIndexCode,beginTime,endTime);
        String hlsPlaybackUrl=HikvisionUtil.callPlaybackURL(cameraIndexCode,beginTime,endTime,HikvisionEnums.PROTOCOL.HLS);
        String rtmpPlaybackUrl=HikvisionUtil.callPlaybackURL(cameraIndexCode,beginTime,endTime,HikvisionEnums.PROTOCOL.RTMP);
        log.info("rtspPlaybackUrl:{},hlsPlaybackUrl:{},rtmpPlaybackUrl:{}",new Object[]{rtspPlaybackUrl,hlsPlaybackUrl,rtmpPlaybackUrl});
        String rtspPlaybackResult=jsonFormat(rtspPlaybackUrl);
        String hlsPlaybackUrlResult=jsonFormat(hlsPlaybackUrl);
        String rtmpPlaybackResult=jsonFormat(rtmpPlaybackUrl);
        return rtspPlaybackResult+"\n"+hlsPlaybackUrlResult+"\n"+rtmpPlaybackResult;
    }

    //==============================================================> end ==============================================================>

    @RequestMapping(value="/add.htm",method ={RequestMethod.POST})
    @ApiOperation(value = "添加摄像头")
    public String add(Camera camera){
        int count=cameraService.addCamera(camera);
        log.info("=============>调用 add 方法，受影响的行数:{}条。",new Object[]{count});
        return "ok";
    }

    @RequestMapping(value="/delete.htm",method ={RequestMethod.POST})
    @ApiOperation(value = "删除摄像头")
    @ApiImplicitParams({@ApiImplicitParam( name = "id", value = "id", required = true)})
    public String delete(Long id){
        int count=cameraService.deleteById(id);
        log.info("=============>调用 delete 方法，受影响的行数:{}条。",new Object[]{count});
        return "ok";
    }

    @RequestMapping(value="/upadte.htm",method ={RequestMethod.POST})
    @ApiOperation(value = "更新摄像头")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "java.lang.Long", name = "id", value = "id", required = true)})
    public String upadte(Camera camera){
        int count=cameraService.update(camera);
        log.info("=============>调用 upadte 方法，受影响的行数:{}条。",new Object[]{count});
        return "ok";
    }

    @RequestMapping(value="/query.htm",method ={RequestMethod.POST})
    @ApiOperation(value = "查询摄像头")
    public String query(Camera camera){
        List<Camera> cameraList=cameraService.query(camera);
        log.info("=============>调用 query 方法.");
        return JSONObject.toJSONString(cameraList);
    }

    private String jsonFormat(String jsonStr){
        JSONObject jsonObject=JSONObject.parseObject(jsonStr);
        String result=JSONObject.toJSONString(jsonObject,true);
        return result;
    }

}
